//
//  SelectPhotoViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/1/30.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit
import Toast_Swift

class SelectPhotoViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    fileprivate var selectImage : UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        self.title = "图片选择"
        
        selectImage = UIImageView.init(frame: CGRect.init(x: 15, y: self.navgationBarBottom + 15, width: MAINSCREEN_WIDTH-15*2, height: MAINSCREEN_HEIGHT/3.0))
        selectImage.image = UIImage(named:"me_placeholder_picture")
        selectImage.contentMode = .scaleAspectFit
        self.view.addSubview(selectImage)
        
        let btnClick = UIButton.init(frame: CGRect.init(x: selectImage.left, y: selectImage.bottom + 15, width: selectImage.width, height: 40))
        btnClick.backgroundColor = UIColor.defaultRedColor
        btnClick.layer.cornerRadius = 5
        btnClick.layer.masksToBounds = true
        btnClick.setTitle("获取照片", for: .normal)
        btnClick.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btnClick.setTitleColor(UIColor.white, for: .normal)
        btnClick.addTarget(self, action: #selector(buttonClick(sender:)), for: .touchUpInside)
        self.view.addSubview(btnClick)
        
    }
    
    @objc func buttonClick(sender : UIButton) -> () {
        
        self.showAlert(aStyle: .actionSheet, aTitle: "", aMessage: "需要访问你的相册或相机来选取照片", aStr: "打开相册", bStr: "打开相机", aCancel: "取消") {
            [weak self]  (aTag:Int) in
            
            let picker:UIImagePickerController = UIImagePickerController()
            picker.delegate = self
            picker.allowsEditing = true
            
            switch aTag {
            case 0: //相册
                picker.sourceType = .photoLibrary
                if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                    self?.present(picker, animated: true, completion: {
                        () -> Void in
                    })
                }else{
                    self?.view.makeToast("没有访问相册的权限", duration: 2.0, position: .center)
                    return
                }
            case 1://相机
                picker.sourceType = .camera
                if UIImagePickerController.isSourceTypeAvailable(.camera) {
                    self?.present(picker, animated: true, completion: {
                        () -> Void in
                    })
                }else{
                    self?.view.makeToast("没有访问相机的权限", duration: 2.0, position: .center)
                    return
                }
            case 2:
                print("取消")
            default:
                print("没有对应点击")
            }
        }
    }

    //MARK: UIImagePickerControllerDelegate
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        if let chooseImage =  info[UIImagePickerControllerOriginalImage] as? UIImage {
            picker.dismiss(animated: true, completion: {
                self.selectImage.image = chooseImage
                //处理传入后台......
            })
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
